home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / stuffit.arc / CPYFILE.ASM < prev    next >
Encoding:
Assembly Source File  |  1985-11-18  |  3.3 KB  |  157 lines

  1. comment *
  2.  
  3.     Copy file
  4.  
  5. 6.12.85
  6.  
  7. Entry:
  8.     BX    
  9.     CX    max bytes to read (buffer size)
  10.     DX    address of transfer buffer
  11.     SI    ASCIIZ string of source file
  12.     DI    ASCIIZ string of destination file
  13.  
  14.  
  15. Error returns in AX (when carry set):
  16.     
  17.     2    File not found
  18.     3    Path not found
  19.     4    Too many open files (no handles left
  20.     5    Access denied (tried to write to existing file?)
  21.     6    Invalid handle
  22.     12    Invalid access code    
  23.     99    Special error, message in this routine
  24.     
  25. Notes:
  26.     Will overwrite to-file of the same name
  27.  
  28.  
  29. *
  30.  
  31. cpyfile        proc    near
  32.         jmp    cpyentry
  33. from_handle    dw    0
  34. to_handle    dw    0
  35. bytes        dw    0
  36. buf_addr    dw    0q
  37. wrt_err_msg    db    cr,lf,'CPY: Destination write error',cr,lf,eos
  38.  
  39. cpyentry:
  40.         mov    bytes,cx    ;save buffer size
  41.         mov    buf_addr,dx    ;save buffer address    
  42. open_from:    mov    dx,si        ;open from-file
  43.         mov    al,0        ;open for read only
  44.         mov    ah,3dh        ;open a file function call
  45.         int    21h
  46.         jnc    ok_from_open
  47.         jmp    cpy_erret
  48. ok_from_open:
  49.         mov    from_handle,ax
  50. create_to:
  51.         mov    cx,0        ;normal attribute
  52.         mov    dx,di        ;to_file ASCIIZ string
  53.         mov    ah,3ch        ;create a file function call
  54.         int    21h
  55.         jnc    ok_to_create
  56.         push    ax        ;save error return code
  57.         mov    bx,from_handle    ;close from-file first 
  58.         mov    ah,3eh        ;close file handle function call
  59.         int    21h
  60.         pop    ax        ;restore prior error code and..
  61.                     ;..ignore any error from file close
  62.         jmp    cpy_erret
  63.  
  64. ok_to_create:
  65.         mov    to_handle,ax    ;save to-file handle
  66. do_read:    mov    bx,from_handle    ;set to copy from-file
  67.         mov    cx,bytes    ;CX has bytes to read
  68.         mov    dx,buf_addr    ;DX has destination buffer
  69.         mov    ah,3fh        ;read from file function call
  70.         int    21h
  71.         jnc    ok_read
  72.         push    ax        ;save error code
  73.         call    close_files
  74.         pop    ax        ;restore original error code
  75.         jmp    cpy_erret
  76. ok_read:
  77.         cmp    ax,0        ;if no bytes read, then tried to read..
  78.                     ;..from end of file, so done copy
  79.         jne    do_write
  80.         jmp    cpy_ok_ret
  81. do_write:    
  82.         mov    bx,to_handle    ;set out-file
  83.         mov    cx,ax        ;CX to bytes to write
  84.                     ;DX has buffer address
  85.         mov    ah,40h        ;write to file function call
  86.         int    21h
  87.         jnc    chk_wrt
  88.         push    ax        ;save error code
  89.         call    close_files
  90.         pop    ax
  91.         jmp    cpy_erret 
  92.                 
  93. chk_wrt:
  94.         cmp    ax,cx        ;chk if actual = requested
  95.         je    ok_write
  96.         call    close_files
  97.         mov    dx,offset wrt_err_msg
  98.         mov    ah,9h        ;print string function call
  99.         int    21h
  100.         mov    ax,99        ;special return code    
  101.         jmp    cpy_erret        
  102.  
  103. ok_write:
  104.         jmp    do_read
  105. cpy_ok_ret:
  106. set_datetime:
  107. get_date:
  108.         mov    bx,from_handle
  109.         mov    al,0        ;get date function
  110.         mov    ah,57h        ;get/set date and time function
  111.         int    21h
  112.         jnc    ok_get_date
  113.         push    ax        ;save error code
  114.         call    close_files
  115.         pop    ax
  116.         jmp    cpy_erret        
  117. ok_get_date:
  118.         mov    bx,to_handle
  119.                     ;CX has time
  120.                     ;DX has date
  121.         mov    al,1        ;set date function
  122.         mov    ah,57h        ;get/set date and time function                
  123.         int    21h
  124.         jnc    done
  125.         push    ax
  126.         call    close_files
  127.         pop    ax
  128.         jmp    cpy_erret    
  129.     
  130. done:
  131.         call    close_files
  132.             ret     
  133. cpy_erret:
  134.         cmp    ax,99        ;special write error code
  135.         je    ce2
  136.         call    errmsg
  137. ce2:        stc            ;set CY 
  138.         ret
  139.  
  140. close_files    proc    near
  141. close_infile    proc    near
  142.         mov    bx,from_handle
  143.         mov    ah,3eh
  144.         int    21h
  145. close_infile    endp
  146.  
  147. close_outfile    proc    near
  148.         mov    bx,to_handle
  149.         mov    ah,3eh
  150.         int    21h
  151. close_outfile    endp
  152.  
  153.         ret
  154. close_files    endp
  155.  
  156. cpyfile        endp
  157.